package it.antonio.ilpizze.db.core.perst;

import it.antonio.ilpizze.db.Id;
import it.antonio.ilpizze.db.core.id.FilePositionId;
import it.antonio.ilpizze.db.core.id.RecordFilePositionId;
import it.antonio.ilpizze.db.index.utils.FPerst;
import it.antonio.ilpizze.db.io.IOConstants;
import it.antonio.ilpizze.db.io.IOFile;

public class IdPerst implements FPerst<Id>, IOConstants {

	@Override
	public long space() {
		return SIZE_LONG;
	}

	@Override
	public void persist(IOFile file, Id id) {
		if (id == null) {
			file.writeLong(-1);
		} else {
			file.writeLong(((FilePositionId)id).pos());
		}
	}

	@Override
	public Id read(IOFile file) {
		long pos = file.readLong();
		if (pos == -1) {
			return null;
		} else {
			return RecordFilePositionId.atPosition(pos);
		}
	}

}